package com.shsxt.crm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shsxt.common.util.JsonUtil;
import com.shsxt.common.util.Md5Util;
import com.shsxt.common.util.Result;
import com.shsxt.common.util.StringUtil;
import com.shsxt.common.util.VerificationLoginUtil;
import com.shsxt.crm.dao.YgUserDao;
import com.shsxt.crm.dao.YgUserPermissionDao;
import com.shsxt.crm.dao.YgUserRoleDao;
import com.shsxt.crm.model.YgUser;
import com.shsxt.crm.model.YgUserPermission;
import com.shsxt.crm.model.YgUserRole;
import com.shsxt.crm.service.IUserService;
import com.shsxt.framework.constant.CrmConstant;
import com.shsxt.framework.constant.ExceptionConstant;
import com.shsxt.framework.exception.YgException;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired(required = false)
	private HttpSession session;

	@Autowired(required = false)
	private HttpServletResponse response;

	@Autowired
	private YgUserDao userDao;

	@Autowired
	private YgUserRoleDao userRoleDao;

	@Autowired
	private YgUserPermissionDao userPermissionDao;

	@Override
	public Result userLogin(String userName, String userPwd) {

		if (StringUtil.isEmpty(userName) || StringUtil.isEmpty(userPwd)) {
			// 用户名 密码为null
			throw new YgException(ExceptionConstant.CODEEXU001, ExceptionConstant.EXU001);
		}

		Map<String, Object> params = new HashMap<>();
		// 查询条件
		params.put("userName", userName);
		YgUser user = userDao.findOne(params);

		if (null != user && Md5Util.encode(userPwd).equals(user.getUserPwd())) {// 校验成功
			// 完成校验 登陆存储数据
			VerificationLoginUtil.ULoginTools(response, session, JsonUtil.parseObjectToString(user));
			queryUserPermission(user.getId());// 用户完成 添加权限
		} else {
			throw new YgException(ExceptionConstant.CODEEXU002, ExceptionConstant.EXU002);
		}
		return Result.success("登陆成功");
	}

	/**
	 * 查询用户的权限
	 * 
	 * @param uid
	 */
	public void queryUserPermission(Integer uid) {

		Map<String, Object> params = new HashMap<>();
		params.put("userId", uid);
		// 获取到用户角色
		YgUserRole role = userRoleDao.findOne(params);

		// 查询 权限
		Map<String, Object> paramsPermission = new HashMap<>();
		paramsPermission.put("roleId", role.getRoleId());
		List<YgUserPermission> permissions = userPermissionDao.find(paramsPermission);

		// 取出用户权限值
		Iterator<YgUserPermission> itor = permissions.iterator();
		// acl值
		List<String> permissionList = new ArrayList<>();

		while (itor.hasNext()) {
			YgUserPermission permission = itor.next();
			permissionList.add(permission.getAclValue());
		}
		session.setAttribute(CrmConstant.USER_PERMISSIONS, permissionList);
	}

	@Override
	public List<YgUser> queryAllCustomerManager() {
		return userDao.queryAllCustomerManager();
	}

	@Override
	public PageInfo<YgUser> querySysUser(YgUser ygUser) {

		PageHelper.startPage(ygUser.getPage(), ygUser.getRows());
		
		Map<String, Object> params = new HashMap<>();

		//	 设置查询参数
		if (ygUser.getUserName() != null && !"".equals(ygUser.getUserName())) {
			params.put("userName", ygUser.getUserName());
		}
		if (ygUser.getPhone() != null && !"".equals(ygUser.getPhone())) {
			params.put("phone", ygUser.getPhone());
		}

		params.put("isValid", 1);
		List<YgUser> list = userDao.find(params);

		return new PageInfo<>(list);
	}

}
